<?php
/**
 +------------------------------------------------------------------------------
 * Core view实现
 +------------------------------------------------------------------------------
 * @version 1.0
 * @author  WangXian
 * @package ephp
 * @filename view.class.php
 * @creation date 2010-8-13 10:22:08
 +------------------------------------------------------------------------------
 */
class View
{
	//模板变量assign
	private $tVar=array();
	/**
     +----------------------------------------------------------
     * 渲染模板
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $file 模板名称
     +----------------------------------------------------------
     */
	public function render($file='')
	{
		//默认渲染当前控制器、当前action的.php文件
		if(empty($file)) $file = $_GET['controller'] . '/' . $_GET['action'].'.tpl.php';
		//模板全路径
		$filename = APP_PATH . '/views/'.$file;
		
		if(! file_exists($filename))
		{
			throw new ephpException("视图文件 {$filename} 不存在，请检查以确认。", 5001);
		}
		
		/** 释放assign的模板变量 **/
		if(! empty($this->tVar))
			extract($this->tVar);
			
		/* 使可以使用$view->xxx,代替$this->xxx保持和controller里的一致 */
		$view = $this;
			
		/** 渲染模板 **/
		if(! C('strip_html_whitespace'))
			include $filename;
		else
		{
			ob_start();
			include $filename;
			echo preg_replace(array('/[ ]{2}/', '/[\r\n]/'),array(' ',''), ob_get_clean());
		}
		
	}
	
	/**
	 * 渲染模板视图
	 * render()的别名
	 * @param string $file
	 * @return void
	 */
	public function display($file='')
	{
		$this->render($file);
		return 0;
	}
	
	/**
     +----------------------------------------------------------
     * 模板变量赋值
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $name
     * @param mixed $value array || string
     +----------------------------------------------------------
     */
    public function assign($name,$value='')
    {
    	 $this->tVar[$name] = $value;
    }
}